package com.hao.loading;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.hao.loadingpopupwindow.R;

/**
 * Created by hao on 16/10/19.
 */
public class LoadingUtils implements View.OnClickListener {

    /**
     * 重新加载的回调
     */
    public interface onAgainLoadingListenter {
        void againLoading();
    }

    private onAgainLoadingListenter onAgainLoadingListenter;


    private View mView;
    private RelativeLayout mLoading;
    private RelativeLayout mLoadingNull;
    private RelativeLayout mLoadingFailed;

    private LoadingUtils() {
    }

    public void setOnAgainLoadingListenter(LoadingUtils.onAgainLoadingListenter onAgainLoadingListenter) {
        this.onAgainLoadingListenter = onAgainLoadingListenter;
    }

    /**
     * 创建对象
     *
     * @param activity 显示view
     */
    public LoadingUtils(Activity activity) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.loading_popup, null);
        mLoading = (RelativeLayout) mView.findViewById(R.id.loading);
        mLoadingNull = (RelativeLayout) mView.findViewById(R.id.loading_null);
        mLoadingFailed = (RelativeLayout) mView.findViewById(R.id.loading_failed);
        mLoading.addView(initLoading(activity));
        mLoading.getChildAt(0).getLayoutParams().width = RelativeLayout.LayoutParams.MATCH_PARENT;
        mLoading.getChildAt(0).getLayoutParams().height = RelativeLayout.LayoutParams.MATCH_PARENT;
        mLoadingFailed.addView(initLoadingFailed(activity));
        mLoadingFailed.getChildAt(0).getLayoutParams().width = RelativeLayout.LayoutParams.MATCH_PARENT;
        mLoadingFailed.getChildAt(0).getLayoutParams().height = RelativeLayout.LayoutParams.MATCH_PARENT;
        mLoadingNull.addView(initLoadingNull(activity));
        mLoadingNull.getChildAt(0).getLayoutParams().width = RelativeLayout.LayoutParams.MATCH_PARENT;
        mLoadingNull.getChildAt(0).getLayoutParams().height = RelativeLayout.LayoutParams.MATCH_PARENT;
    }

    public View getView() {
        return mView;
    }

    //初始化默认加载布局
    private View initLoading(Activity activity) {
        View view = LayoutInflater.from(activity).inflate(R.layout.loading, null);
        return view;
    }

    //初始化默认加载为null布局
    private View initLoadingNull(Activity activity) {
        View view = LayoutInflater.from(activity).inflate(R.layout.loading_null, null);
        return view;
    }

    //初始化默认加载失败布局
    private View initLoadingFailed(Activity activity) {
        View view = LayoutInflater.from(activity).inflate(R.layout.loading_failed, null);
        view.findViewById(R.id.again_loading).setOnClickListener(this);
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.again_loading:
                if (onAgainLoadingListenter == null)
                    return;
                showLoading();
                onAgainLoadingListenter.againLoading();
                break;
            default:
                break;
        }

    }

    /**
     * 显示loading
     */
    public LoadingUtils showLoading() {
        if (mLoadingFailed.getVisibility() == View.VISIBLE)
            mLoadingFailed.setVisibility(View.GONE);
        if (mLoadingNull.getVisibility() == View.VISIBLE)
            mLoadingNull.setVisibility(View.GONE);
        if (mLoading.getVisibility() == View.GONE)
            mLoading.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 显示loadingNull
     */
    public LoadingUtils showLoadingNull() {
        if (mLoadingNull.getVisibility() == View.GONE)
            mLoadingNull.setVisibility(View.VISIBLE);
        if (mLoadingFailed.getVisibility() == View.VISIBLE)
            mLoadingFailed.setVisibility(View.GONE);
        if (mLoading.getVisibility() == View.VISIBLE)
            mLoading.setVisibility(View.GONE);
        return this;
    }

    /**
     * 显示loadingFailed
     */
    public LoadingUtils showLoadingFailed() {
        if (mLoadingNull.getVisibility() == View.VISIBLE)
            mLoadingNull.setVisibility(View.GONE);
        if (mLoading.getVisibility() == View.VISIBLE)
            mLoading.setVisibility(View.GONE);
        if (mLoadingFailed.getVisibility() == View.GONE)
            mLoadingFailed.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 添加自己的loading View
     *
     * @param view
     */
    public LoadingUtils addLoadingView(View view) {
        if (mLoading.getChildCount() > 0)
            mLoading.removeAllViews();
        mLoading.addView(view);
        return this;
    }

    /**
     * 添加自己的loadingNull View
     *
     * @param view
     */
    public LoadingUtils addLoadingNullView(View view) {
        if (mLoadingNull.getChildCount() > 0)
            mLoadingNull.removeAllViews();
        mLoadingNull.addView(view);
        return this;
    }

    /**
     * 添加自己的loadingFailed View
     *
     * @param view
     */
    public LoadingUtils addLoadingFailedView(View view) {
        if (mLoadingFailed.getChildCount() > 0)
            mLoadingFailed.removeAllViews();
        mLoadingFailed.addView(view);
        return this;
    }


    /**
     * 销毁处 销毁所有的view释放内存 防止内存益处
     */

    public void onDestroy() {
        mView = null;
        mLoading = null;
        mLoadingNull = null;
        mLoadingFailed = null;
    }
}
